﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Calendar;
using Journal.Helpers;

namespace Journal.Domain
{
    public class Calendar : ICloneable
    {
        private IList<Event> _events = new List<Event>();

        public virtual Guid Id { get; set; }
        public virtual bool Primary { get; set; }
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual string Location { get; set; }
        public virtual IList<Event> Events
        {
            get { return _events; }
            set { _events = value; }
        }

        #region Google
        private CalendarEntry gcal = null;
        public CalendarEntry GoogleCalendar
        {
            get { return gcal; }
            set
            {
                gcal = value;
                this.Name = gcal.Title.Text;
                this.Description = gcal.Summary.Text;
                if (gcal.Location != null)
                    this.Location = gcal.Location.ValueString;
                this.Primary = Utilities.IsGoogleCalendarPrimary(gcal);
                isGoogle = true;
            }
        }
        private bool isGoogle = false;
        public bool IsGoogleCalendar
        {
            get
            {
                return this.isGoogle;
            }
        }
        #endregion

        public object Clone()
        {
            Calendar clone = new Calendar();
            clone.Id = this.Id;
            clone.Primary = this.Primary;
            clone.Name = this.Name;
            clone.Description = this.Description;
            clone.Location = this.Location;
            clone.Events = this.Events;
            clone.gcal = this.gcal;
            return clone;
        }

        public bool IsSameAs(System.Object obj)
        {
            Calendar cal = obj as Domain.Calendar;
            if ((object)cal == null) return false;

            if (cal.IsGoogleCalendar) return this.gcal.Id.Equals(cal.gcal.Id);
            else return this.Id.Equals(cal.Id);
        }
    }
}
